﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Eis51.CampanyCMS.Model.DownLoadMgnt;
using Eis51.CampanyCMS.Business.DownLoadMgnt;
using Eis51.CampanyCMS.Model;
using System.IO;
using Eis51.CampanyCMS.Business.Bus;
using System.Collections;
using Eis51.CampanyCMS.Model.NewsManager.QueryO;
using Eis51.CampanyCMS.Business.NewsManager;

namespace Eis51.Web.Admin.DownLoadFileMgnt
{
    public partial class edit_download : BasePage
    {
        private int DownLoadFileId
        {
            get
            {
                if (Request.QueryString["DownLoadFileId"] == null)
                {
                    return 0;
                }
                else
                {
                    return int.Parse(Request.QueryString["DownLoadFileId"]);
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //btnDelFile.Visible = false;
                InitData();
            }
        }

        private void InitData()
        {
            //ddlType.DataSource = DownloadFileTypeBLL.GetInstance().Select();
            //ddlType.DataTextField = "TypeName";
            //ddlType.DataValueField = "DownLoadFileTypeID";
            //ddlType.DataBind();


            DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key((DownLoadFileId)));
            if (obj != null)
            {
                txtBrief.Text = obj.Brief;
                txtTitle.Text = obj.Title;
                txtShowIdx.Text = obj.ShowIdx.ToString();
                
                //txtContent.Value = obj.Content;
                ddlType.SelectedValue = obj.ClassType;
                BindPosition(obj.Code);               

                //this.btnDelFile.Visible = obj.FilePath1 != "";
                this.btnDelLogo.Visible = obj.FileLogo != "";
                if (obj.FileName1!="")
                {
                    //lnkFile1.Text = obj.FileName1;
                    //File_path1[文件地址1（固定在/UploadFile/|/File/|FilePath1|）]
                    //lnkFile1.NavigateUrl = Page.ResolveClientUrl("~/UploadFile/File/" + obj.FilePath1);
                }

                if (obj.FileLogo != "")
                {
                    lnkLogo.Text = "<img style='width:150px;height:150px' src=\"" + Page.ResolveClientUrl("~/UploadFile/Img/" + obj.FileLogo) + "\" alt=''/>";
                    //File_path1[文件地址1（固定在/UploadFile/|/File/|FileLogo|）]                    
                }
            }
            
        }        

        private void BindPosition(string code)
        {
            IList list = null;
            string scondition = string.Empty;
            switch (int.Parse(ddlType.SelectedValue))
            {
                case (int)IndexImgPosition.Product:
                    scondition = " Isfirst>0";
                    list = ProductsBLL.GetInstance().Search(scondition); 
                    ddlPid.DataSource = list;
                    ddlPid.DataTextField = "Title";
                    ddlPid.DataValueField = "Seqno";
                    break;
                case (int)IndexImgPosition.Customer:
                    CustomerQuery customerQuery = new CustomerQuery();
                    customerQuery.IsFist = true;
                    list = CustomerBLL.GetInstance().Search(customerQuery);
                    ddlPid.DataSource = list;
                    ddlPid.DataTextField = "Title";
                    ddlPid.DataValueField = "Seqno";
                    break;
                case (int)IndexImgPosition.Purchase:
                    scondition = " Isfirst>0";
                    list = PurchaseBLL.GetInstance().Search(scondition);
                    ddlPid.DataSource = list;
                    ddlPid.DataTextField = "Title";
                    ddlPid.DataValueField = "ProductID";
                    break;
                case (int)IndexImgPosition.News:
                    NewsQuery newsQuery = new NewsQuery();
                    newsQuery.IsFist = true;
                    list = NewsBLL.GetInstance().Search(newsQuery);
                    ddlPid.DataSource = list;
                    ddlPid.DataTextField = "Title";
                    ddlPid.DataValueField = "Seqno";
                    break;
                case (int)IndexImgPosition.Market:
                    MarketQuery marketQuery = new MarketQuery();
                    marketQuery.IsFist = true;
                    list = MarketBLL.GetInstance().Search(marketQuery);
                    ddlPid.DataSource = list;
                    ddlPid.DataTextField = "Title";
                    ddlPid.DataValueField = "Seqno";
                    break;

            }
            if (null != list && list.Count > 0)
            {
                ddlPid.DataBind();
                if (!string.IsNullOrEmpty(code))
                {
                    ddlPid.SelectedValue = code;
                }
            }            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key((DownLoadFileId)));

            if (obj == null)
            {
                obj = new DownloadFile();
            }
            obj.Brief = txtBrief.Text;
            obj.Title = txtTitle.Text;
            obj.ShowIdx = int.Parse(txtShowIdx.Text);
            //obj.Content = txtContent.Value;
            obj.ClassType = ddlType.SelectedValue;
            obj.Createby = AppContext.CurrentAdministrator.UserName;
            obj.Createdate = DateTime.Now;
            obj.Code = ddlPid.SelectedValue;
            //if (FileUpload1.FileName != string.Empty)
            //{
                
            //    string savePath = string.Format("{0}/{1}/{2}"
            //        , DateTime.Now.ToString("yyyy")
            //        , DateTime.Now.ToString("MM")
            //        , DateTime.Now.ToString("dd"));

            //    //File_path1[文件地址1（固定在/UploadFile/|/File/|File_path1|）]
            //    string patch = string.Format("~/UpLoadFile/{0}/File/{1}" 
            //        ,AppContext.CurrentAdministrator.UserName,
            //        savePath);
                    
            //    string fileName = FileListBLL.SaveFile(FileUpload1, Server.MapPath(patch));
            //    savePath = Path.Combine(savePath, fileName);
            //    obj.FilePath1 = savePath;

            //    FileInfo info =  new FileInfo(FileUpload1.PostedFile.FileName);
            //    obj.FileName1 = info.Name;

            //}

            if (fuFileLogo.FileName != string.Empty)
            {
                string savePath = string.Format("{0}/{1}/{2}"
                    , DateTime.Now.ToString("yyyy")
                    , DateTime.Now.ToString("MM")
                    , DateTime.Now.ToString("dd"));

                //File_path1[文件地址1（固定在/UploadFile/|/File/|File_path1|）]
                string patch = string.Format("~/UpLoadFile/Img/{1}"
                    , AppContext.CurrentAdministrator.UserName,
                    savePath);

                string fileName = FileListBLL.SaveFile(fuFileLogo, Server.MapPath(patch));
                savePath = Path.Combine(savePath, fileName);
                obj.FileLogo = savePath;
            }


            DownloadFileBLL.GetInstance().Submit(obj);
            btnReturn_Click(null, null);

        }



        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.QueryString["r"]);
        }

        protected void btnDelFile_Click(object sender, EventArgs e)
        {
            DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key((DownLoadFileId)));
            //File_path1[文件地址1（固定在/UploadFile/|/File/|File_path1|）]
            string path = Server.MapPath("~/UploadFile/" + obj.Createby + "/File/" + obj.FilePath1);
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            //lnkFile1.NavigateUrl = "";
            //lnkFile1.Text = "";
            //lnkFile1.Visible = false;
            //btnDelFile.Visible = false;
            if (obj != null)
            {
                obj.FilePath1 = "";
                obj.FileName1 = "";
                DownloadFileBLL.GetInstance().Submit(obj);
            }           
        }

        protected void btnDelLogo_Click(object sender, EventArgs e)
        {
            DownloadFile obj = DownloadFileBLL.GetInstance().Get(new DownloadFile.Key((DownLoadFileId)));
            //File_path1[文件地址1（固定在/UploadFile/|/File/|File_path1|）]
            string path = Server.MapPath("~/UploadFile/" + obj.Createby + "/Img/" + obj.FileLogo);
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            lnkLogo.NavigateUrl = "";
            lnkLogo.Text = "";
            lnkLogo.Visible = false;
            btnDelLogo.Visible = false;
            if (obj != null)
            {
                obj.FileLogo = "";                
                DownloadFileBLL.GetInstance().Submit(obj);
            }           
        }

        protected void ddlType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindPosition(string.Empty);
        }
    }
}
